﻿using System;
using System.Web;
using System.Web.Configuration;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using BluePortal.LiveID;

namespace BluePortal.QuikStart
{
    public partial class BluePortalMaster : System.Web.UI.MasterPage
    {
        protected string AppID;
        protected string RedirectLogin;
        public ScriptManager scriptManager
        {
            get {
                return ScriptManager1;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            
            //Initialize 
            RedirectLogin = WebConfigurationManager.AppSettings["redirectLogin"];
            
            WindowsLiveAuth wll = new WindowsLiveAuth(true);
            AppID = wll.AppId;
            if (Page.User.Identity.IsAuthenticated)
            {
                ((ImageButton)LoginView1.FindControl("ImageButtonLogin")).ImageUrl = "/Images/signout.gif";
                
            }
            else
            {
                ((ImageButton)LoginView1.FindControl("ImageButtonLogin")).ImageUrl = "/Images/signin.gif";
                
            }
            //this page is currently handling login and logout
            //check what action it needs to perform by looking at the query string
            HttpRequest req = HttpContext.Current.Request;
            string action = req.QueryString.Get("action");
            if (action == "clearcookie")
            {
                /* This action is called when the user tries to log-out.  */
                // insert your clear cookie logic here */
                // after cookies are cleared, you must reply to Windows Live ID server 
                // Confirm successful sign out by sending an http response.
                byte[] content;
                string type;
                wll.GetClearCookieResponse(out type, out content);
                HttpResponse res = HttpContext.Current.Response;
                res.ContentType = type;
                res.OutputStream.Write(content, 0, content.Length);
                res.End();
            }
            else
            // we know its not a sign out, so process normally.  
            // Here we will print out the user ID if the user is signed in.
            {
                if (!Page.User.Identity.IsAuthenticated)
                {
                    WindowsLiveAuth.User user = wll.ProcessLogin(req.Form);
                    if (user != null)
                    {
                        //LabelGreeting.Text = "Hello there! Thanks for signing in.  Your user ID is: " + user.Id;
                        LiveIdService lidService = new LiveIdService();
                        string loginName = lidService.FindLoginName(user.Id);
                        if ( loginName == string.Empty)
                        {
                            Session["NewLiveId"] = user.Id;
                            Response.Redirect("EditUser.aspx");
                        }
                        else
                        {
                            FormsAuthentication.RedirectFromLoginPage(loginName, false);
                        }
                    }
                }
            }
        }

        public void SignIn()
        {
            Response.Redirect(RedirectLogin + "/login.srf?appid=" + AppID);
        }
        public void SignOut()
        {
            FormsAuthentication.SignOut();
            Response.Redirect(RedirectLogin + "/logout.srf?appid=" + AppID);
        }


        protected void ImageButtonLogin_Click(object sender, ImageClickEventArgs e)
        {
            if (Page.User.Identity.IsAuthenticated)
            {
                SignOut();
            }
            else
            {
                SignIn();
            }
        }
    }
}
